package com.zhenhao.xsq.base;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.BarUtils;
import com.orhanobut.logger.Logger;
import com.zhenhao.xsq.R;
import com.zhenhao.xsq.utils.SpSaveUtils;
import com.zhenhao.xsq.utils.UIUtils;
import com.zhenhao.xsq.view.ProgressDialog;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;


/**
 * @author Yuan.Y.Q
 * @Date 2017/9/14.
 */

public abstract class BaseActivity extends AppCompatActivity {
    public Toolbar mToolbar;
    public LinearLayout autolayout;
    public boolean needFront = false;   //toolBar 是否需要显示在最上层的标识
    public ProgressDialog progressDialog;
    public TextView toolbarTitle;
    public TextView toolbarMenu;
    public ImageView toolimgMenu;
    public RelativeLayout rootLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWindows();
        super.setContentView(R.layout.activity_base);
        progressDialog = new ProgressDialog(this);
        initToolbar();
        setupActivityComponent();
        initView();
        initData();
//        testWidthDB();

    }

    private void initWindows() {
        //4.4版本及以上 最低版本设置为21
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager
                    .LayoutParams
                    .FLAG_TRANSLUCENT_STATUS);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager
                    .LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View
                    .SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.BLACK);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //android6.0以后可以对状态栏文字颜色和图标进行修改 oo
            getWindow().getDecorView().setSystemUiVisibility(View
                    .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    private void initToolbar() {
        mToolbar = findViewById(R.id.toolbarBusiness);
        toolbarMenu = findViewById(R.id.tv_menu);
        toolimgMenu = findViewById(R.id.img_menu);
        autolayout = findViewById(R.id.toolbar_autolayout);
        mToolbar.setTitle("");
        toolbarTitle = findViewById(R.id.tv_toolbar_title);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(UIUtils.getDisplayWidth(this),
                UIUtils.getStatusBarHeight(this) * 3);

        Logger.e("------------状态栏高度" + BarUtils.getStatusBarHeight() + "actionbar" + BarUtils
                .getActionBarHeight());

        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
        }

        autolayout.setLayoutParams(layoutParams);
        autolayout.setPadding(0, (UIUtils.getStatusBarHeight(this)), 0, 0);

        switch (SpSaveUtils.getToolbarColorNum()) {
            case 0:
                autolayout.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
                mToolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
                toolbarTitle.setTextColor(ContextCompat.getColor(this, R.color.color_333333));
                toolbarMenu.setTextColor(ContextCompat.getColor(this, R.color.color_2d97ff));
                mToolbar.setNavigationIcon(ContextCompat.getDrawable(this, R.mipmap.toolbar_back2));
                break;
            case 1:
                autolayout.setBackgroundColor(ContextCompat.getColor(this, R.color.color_2d97ff));
                mToolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.color_2d97ff));
                toolbarTitle.setTextColor(ContextCompat.getColor(this, R.color.white));
                toolbarMenu.setTextColor(ContextCompat.getColor(this, R.color.white));
                mToolbar.setNavigationIcon(ContextCompat.getDrawable(this, R.mipmap.toolbar_back));
                break;
            case 2:
                autolayout.setBackgroundColor(ContextCompat.getColor(this, R.color.color_00000000));
                mToolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.color_00000000));
                toolbarTitle.setTextColor(ContextCompat.getColor(this, R.color.white));
                toolbarMenu.setTextColor(ContextCompat.getColor(this, R.color.white));
                mToolbar.setNavigationIcon(ContextCompat.getDrawable(this, R.mipmap.toolbar_back));
                break;
            case 3:
                autolayout.setBackgroundColor(ContextCompat.getColor(this, R.color.color_333333));
                mToolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.color_333333));
                toolbarTitle.setTextColor(ContextCompat.getColor(this, R.color.white));
                toolbarMenu.setTextColor(ContextCompat.getColor(this, R.color.white));
                mToolbar.setNavigationIcon(ContextCompat.getDrawable(this, R.mipmap.toolbar_back));
                break;
        }

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Log.e("BaseActivity", "这里"+v.getId());
//                onBackPressed();
                int id = v.getId();
//                if (id == android.R.id.home) {
                if (id == -1) {
                    onBackPressed();
                }
            }
        });
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setToolbarTitle(String title) {
        if (toolbarTitle != null) {
            toolbarTitle.setText(title);
        }
    }

    @Override
    public void setContentView(int layoutId) {
        setContentView(View.inflate(this, layoutId, null));
    }

    @Override
    public void setContentView(View view) {
        rootLayout = findViewById(R.id.root_layout);


        if (rootLayout == null) {
            return;
        }
        if (needFront) {
            autolayout.setBackgroundColor(getResources().getColor(R.color.color_00000000));
            mToolbar.setBackgroundColor(getResources().getColor(R.color.color_00000000));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup
                    .LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            rootLayout.addView(view, params);
            autolayout.bringToFront();
        } else {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup
                    .LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            params.addRule(RelativeLayout.BELOW, R.id.toolbar_autolayout);
            rootLayout.addView(view, params);
        }
//        initToolbar();
    }

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 初始化view
     */
    protected abstract void initView();

    /**
     * 初始化dagger2
     */
    protected abstract void setupActivityComponent();

    protected <T> void startActivty(Class<T> clazz) {
        Intent intent = new Intent(this, clazz);
        try {
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 启动Activity
     *
     * @param clazz
     */
    protected <T> void startActivity(Class<T> clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        intent.putExtras(bundle);
        try {
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (progressDialog != null) {
            progressDialog.hide();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * 设置菜单
     */
    public void setMenuText(String text, View.OnClickListener onClickListener) {
        toolbarMenu.setText(text);
        toolbarMenu.setVisibility(View.VISIBLE);
        toolbarMenu.setOnClickListener(onClickListener);

    }

    /**
     * 设置图片
     */
    public void setMenuImg(int resousid, View.OnClickListener onClickListener) {
        toolimgMenu.setImageResource(resousid);
        toolimgMenu.setVisibility(View.VISIBLE);
        toolimgMenu.setOnClickListener(onClickListener);

    }

    public void testWidthDB() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int heightPixels = dm.heightPixels;
        int widthPixels = dm.widthPixels;
        float density = dm.density;
        float heightDP = heightPixels / density;
        float widthDP = widthPixels / density;
        float smallestWidthDP;
        if (widthDP < heightDP) {
            smallestWidthDP = widthDP;
        } else {
            smallestWidthDP = heightDP;
        }
        Logger.e(smallestWidthDP + "--------");
    }
}
